<!--
 * @Description:第三方授权 增/改页面
 * @Autor: zhangzhanhua
 * @Date: 2021-04-12 09:46:35
 * @LastEditors: zhangzhanhua
 * @LastEditTime: 2021-11-25 16:34:25
-->
<template>
    <div class="addOrEdit">
        <div class="manage">
            <el-form label-position="right" class="elFormWeb" :model="manageData" :rules="manageDataRule" ref="manageData" v-loading='loading'>
                <el-form-item label="第三方全称" prop="thirdFullName" label-width="120px">
                    <el-input type="text" size='mini' v-model="manageData.thirdFullName"></el-input>
                </el-form-item>
                <el-form-item label="第三方简称" prop="thirdName" label-width="120px">
                    <el-input type="text" size='mini' v-model="manageData.thirdName"></el-input>
                </el-form-item>
                <el-form-item label="状态" label-width="120px" prop="status" v-if="type != 'add'">
                    <el-switch v-model="manageData.status" active-color="#338CE8" inactive-color="#ff4949" active-text="启用" inactive-text="停用" active-value='0' inactive-value='1'>
                    </el-switch>
                </el-form-item>
                <el-form-item label="备注" label-width="120px">
                    <el-input type="textarea" size='mini' v-model="manageData.remark"></el-input>
                </el-form-item>
            </el-form>
            <div class="userBtn">
                <el-button size="small" type="primary" @click="saveAdd" :disabled='loading'>保 存</el-button>
            </div>
        </div>
    </div>

</template>

<script>
	export default {
		components: {

		},
		props: {
			//选中项id
			parentId: {
				type: String | Number,
			},
			//状态
			type: {
				type: String,
				default: 'add'
			},
			// 关闭页面
			close: {
				type: Function
			},
		},
		data() {
			return {
				loading: false,//编辑时防止快速点击
				manageData: {
					thirdName: '', //第三方名称
					status:'', //状态（0：启用，1：停用）
					remark: '', //备注
				},
				//校验规则
				manageDataRule: {
					thirdFullName:[
						{
							required: true,
							message: '第三方名称不能为空',
						}
					],
					thirdName: [
						{
							required: true,
							message: '第三方名称不能为空',
						}
					],
					status: [
						{
							required: true,
						}
					],
				},
			}
		},
		mounted() {
			this.getRowData(); //编辑时获取当前行数据
		},
		methods: {
			/**
			 * @description:编辑时获取该行数据
			 * @param {*}
			 * @return {*}
			 * @author: zhangzhanhua
			 */
			getRowData() {
				if (this.type == 'edit') {
					this.loading = true
					this.$http(`/${this.$ServiceName}/api/external/exthirdauth/${this.$version}/getexthirdauthbyid`, {
						params: {
							id: this.parentId
						}
					}).then(res => {
						if (res.data.success) {
							this.loading = false
							let data = {
								thirdFullName:res.data.data.thirdFullName,
								thirdName: res.data.data.thirdName, //第三方名称
								status: res.data.data.status, //状态（0：启用，1：停用）
								remark: res.data.data.remark, //备注
							}
							this.manageData = data
						} else {
							this.$message.error(res.data.msg)
						}
					})
				}
			},
			/**
			 * @description: 保存新增
			 * @param {*}
			 * @return {*}
			 * @author: zhangzhanhua
			 */
			saveAdd() {
				this.$refs.manageData.validate(valid => {
					if (valid) {
						let params = JSON.parse(JSON.stringify(this.manageData))
						//新增
						if (this.type == 'add') {
							//删除状态
							this.$delete(params,'status')
							this.$http.post(`/${this.$ServiceName}/api/external/exthirdauth/${this.$version}/insert`, params).then(res => {
								if (res.data.success) {
									this.$message.success(res.data.msg)
									this.close()
								} else {
									this.$message.error(res.data.msg)
								}
							})
						} else { //编辑
							params.id = this.parentId
							this.$http.put(`/${this.$ServiceName}/api/external/exthirdauth/${this.$version}/updatebyid`, params).then(res => {
								if (res.data.success) {
									this.$message.success(res.data.msg)
									this.close()
								} else {
									this.$message.error(res.data.msg)
								}
							})
						}
					}
				})

			}
		},
	}
</script>

<style lang="less" scoped>
    .addOrEdit {
        .userBtn {
            z-index: 100;
            text-align: right;
            padding-right: 20px;
            border-top: 1px solid #c8cfde;
            background-color: #f5f7fa;
            position: absolute;
            bottom: 0px;
            left: 0;
            right: 0;
            height: 50px;
            line-height: 50px;
        }
    }
    .manage {
        /deep/ .drawer .el-input__inner {
            width: 100% !important;
        }
        .elFormWeb {
            // height: 654px;
            padding: 10px 30px 50px 10px;
            overflow: auto;
        }
    }
</style>
